package com.ehsunbehravesh.mypasswords;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Date;

public class Logger {

  private static String filename = "log.txt";

  public static void log(String msg) {    
    FileWriter fw = null;
    BufferedWriter bw = null;
    try {
      fw = new FileWriter(filename, true);
      bw = new BufferedWriter(fw);

      Date date = new Date();

      bw.write(date.toString() + ": " + msg);
      bw.newLine();
      bw.flush();
    } catch (IOException ex) {
      ex.printStackTrace();
    } finally {
      try {
        if (bw != null) {
          bw.close();
        }
        fw.close();
      } catch (IOException ex) {
        ex.printStackTrace();
      }
    }
  }

  public static PrintStream getPrintWriter() {
    try {
      return new PrintStream(new File(filename));
    } catch (IOException ex) {
      ex.printStackTrace();
      return null;
    }
  }
}
